package com.kyrie.lib_sys.application.publisherApplys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kyrie.lib_sys.common.entity.PageResult;
import com.kyrie.lib_sys.common.pojo.dto.PublisherDTO;
import com.kyrie.lib_sys.common.pojo.dto.PublisherPageQueryDTO;
import com.kyrie.lib_sys.common.pojo.entity.Publisher;
import com.kyrie.lib_sys.application.publisherApplys.mapper.PublisherMapper;
import com.kyrie.lib_sys.application.publisherApplys.service.PublisherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 出版社service类
 *
 * @author heefM
 * @date 2025-08-09
 */
@Service
public class PublisherServiceImpl implements PublisherService {
    @Autowired

    private PublisherMapper publisherMapper;

    @Override
    public void insert(PublisherDTO publisherDTO) {
        Publisher publisher = new Publisher();
        BeanUtils.copyProperties(publisherDTO, publisher);
        publisherMapper.insert(publisher);
    }

    @Override
    public Publisher queryPublisher(PublisherDTO publisherDTO) {
        Publisher publisher = new Publisher();
        BeanUtils.copyProperties(publisherDTO, publisher);
        return publisherMapper.queryByCondition(publisher);
    }

    @Override
    public PublisherDTO queryPublisherById(Long id) {
        Publisher publisher = publisherMapper.queryById(id);
        if (publisher != null) {
            PublisherDTO publisherDTO = new PublisherDTO();
            BeanUtils.copyProperties(publisher, publisherDTO);
            return publisherDTO;
        }
        return null;
    }

    @Override
    public PageResult queryList(PublisherPageQueryDTO publisherPageQueryDTO) {
        PageHelper.startPage(publisherPageQueryDTO.getPageNum(), publisherPageQueryDTO.getPageSize());
        Page<Publisher> page = publisherMapper.queryList(publisherPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    public PageResult queryPage(PublisherPageQueryDTO publisherPageQueryDTO) {
        PageHelper.startPage(publisherPageQueryDTO.getPageNum(), publisherPageQueryDTO.getPageSize());
        Page<Publisher> page = publisherMapper.queryList(publisherPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    public PublisherDTO queryByCondition(PublisherDTO publisherDTO) {
        Publisher publisher = new Publisher();
        BeanUtils.copyProperties(publisherDTO, publisher);
        Publisher result = publisherMapper.queryByCondition(publisher);
        if (result != null) {
            PublisherDTO resultDTO = new PublisherDTO();
            BeanUtils.copyProperties(result, resultDTO);
            return resultDTO;
        }
        return null;
    }

    @Override
    public void update(PublisherDTO publisherDTO) {
        Publisher publisher = new Publisher();
        BeanUtils.copyProperties(publisherDTO, publisher);
        publisherMapper.update(publisher);
    }

    @Override
    public void deleteById(Long id) {
        publisherMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        publisherMapper.deleteBatch(ids);
    }

    @Override
    public void insertBatch(List<PublisherDTO> publisherDTOS) {
        ArrayList<Publisher> list = new ArrayList<>();
        for (PublisherDTO publisherDTO : publisherDTOS) {
            if (publisherDTO != null) {
                Publisher publisher = new Publisher();
                BeanUtils.copyProperties(publisherDTO, publisher);
                list.add(publisher);
            }
        }
        publisherMapper.insertBatch(list);
    }
}
